import { Vector2 } from "../vector";  
import { Engine } from "../engine";  
/**
 * // 基础文本气泡  
this.engine.player.say("Hello World!", 3000);  
  
// 自定义样式的气泡  
this.engine.player.say("重要消息！", 5000, {  
  backgroundColor: "#ff0000cc",  
  textColor: "#ffffff",  
  fontSize: 16,  
  offset: new Vector2(0, -80)  
});  
  
// 在特定事件时显示气泡  
if (playerCollectedItem) {  
  this.engine.player.say("获得了新物品！", 2000);  
}
 */
export interface BubbleOptions {  
  text: string;  
  duration: number;  
  offset: Vector2;  
  backgroundColor: string;  
  textColor: string;  
  fontSize: number;  
  padding: number;  
  borderRadius: number;  
}  
  
export class TextBubble {  
  private text: string;  
  private startTime: number;  
  private duration: number;  
  private offset: Vector2;  
  private options: BubbleOptions;  
  private isActive: boolean = false;  
  
  constructor(  
    private engine: Engine,  
    text: string,  
    duration: number = 3000,  
    options: Partial<BubbleOptions> = {}  
  ) {  
    this.text = text;  
    this.duration = duration;  
    this.options = {  
      text,  
      duration,  
      offset: new Vector2(0, -60),  
      backgroundColor: "#000000cc",  
      textColor: "#ffffff",  
      fontSize: 14,  
      padding: 8,  
      borderRadius: 8,  
      ...options  
    };  
    this.offset = this.options.offset;  
  }  
  
  show(): void {  
    this.startTime = this.engine.time_;  
    this.isActive = true;  
  }  
  
  hide(): void {  
    this.isActive = false;  
  }  
  
  get isVisible(): boolean {  
    if (!this.isActive) return false;  
    const elapsed = this.engine.time_ - this.startTime;  
    return elapsed < this.duration;  
  }  
  
  get progress(): number {  
    if (!this.isActive) return 0;  
    const elapsed = this.engine.time_ - this.startTime;  
    return Math.min(elapsed / this.duration, 1);  
  }  
  
  render(ctx: CanvasRenderingContext2D, playerPos: Vector2): void {  
    if (!this.isVisible) return;  
  
    ctx.save();  
  
    // 计算气泡位置  
    const bubblePos = new Vector2(  
      playerPos.x + this.offset.x,  
      playerPos.y + this.offset.y  
    );  
  
    // 设置字体  
    ctx.font = `${this.options.fontSize}px Arial`;  
    ctx.textAlign = "center";  
    ctx.textBaseline = "middle";  
  
    // 测量文本尺寸  
    const textMetrics = ctx.measureText(this.text);  
    const textWidth = textMetrics.width;  
    const textHeight = this.options.fontSize;  
  
    // 计算气泡尺寸  
    const bubbleWidth = textWidth + this.options.padding * 2;  
    const bubbleHeight = textHeight + this.options.padding * 2;  
  
    // 应用淡入淡出效果  
    let alpha = 1;  
    if (this.progress < 0.2) {  
      alpha = this.progress / 0.2;  
    } else if (this.progress > 0.8) {  
      alpha = (1 - this.progress) / 0.2;  
    }  
  
    // 绘制气泡背景  
    ctx.globalAlpha = alpha;  
    ctx.fillStyle = this.options.backgroundColor;  
    ctx.beginPath();  

    // 绘制气泡背景  
    ctx.globalAlpha = alpha;  
    ctx.fillStyle = this.options.backgroundColor;  
    ctx.fillRect(  
        bubblePos.x - bubbleWidth / 2,  
        bubblePos.y - bubbleHeight / 2,  
        bubbleWidth,  
        bubbleHeight  
    );
    ctx.fill();  
  
    // 绘制小尾巴（指向角色）  
    const tailSize = 8;  
    ctx.beginPath();  
    ctx.moveTo(bubblePos.x, bubblePos.y + bubbleHeight / 2);  
    ctx.lineTo(bubblePos.x - tailSize / 2, bubblePos.y + bubbleHeight / 2 + tailSize);  
    ctx.lineTo(bubblePos.x + tailSize / 2, bubblePos.y + bubbleHeight / 2 + tailSize);  
    ctx.closePath();  
    ctx.fill();  
  
    // 绘制文本  
    ctx.fillStyle = this.options.textColor;  
    ctx.fillText(this.text, bubblePos.x, bubblePos.y);  
  
    ctx.restore();  
  }  
}